<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Controller;

use Common\Common;
use Common\CommonController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Swagger\Annotation\HyperfServer;
use Plugin\Attractions\AttractionManagement\Aspect\CheckJwtToken;
use Plugin\Attractions\AttractionManagement\Service\BookingService;

#[HyperfServer(name: 'http')]
#[Controller('attractions/attraction-management')]
class BookingController extends CommonController
{ 
    #[Inject]
    private BookingService $service;

    #[PostMapping('booking'), CheckJwtToken]
    public function booking()
    {
        $data = $this->valid([
            'touristId' => 'string|regex:/^\d+(,\d+)*$/|required',
            'infoId' => 'integer|required',
            'bookingDay' => 'date|required',
            'bookingTime' => 'string|required',
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $data['userId'] = $userId;
        $res = $this->service->booking($data);
        return empty($res) ? $this->error() : $this->success($res);
    }

    #[GetMapping('bookingList'), CheckJwtToken]
    public function bookingList()
    {
        $data = $this->valid([
            'status' => 'integer',
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $data['userId'] = $userId;
        return $this->success($this->service->bookingList($data));
    }

    #[PostMapping('cancel'), CheckJwtToken]
    public function cancel()
    {
        $data = $this->valid([
            'id' => 'required|array'
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $data['userId'] = $userId;
        $data['status'] = 2;
        return $this->success($this->service->updateStatus($data));
    }

    #[PostMapping('verify')]
    public function verify()
    {
        $data = $this->valid([
            'spotId' => 'required|integer',
            'code' => 'required|integer'
        ], []);
        $data['status'] = 3;
        return $this->success($this->service->updateStatus($data));
    }

    #[PostMapping('delete')]
    public function delete()
    {
        $data = $this->valid([
            'id' => 'required|array'
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $data['userId'] = $userId;
        return $this->success($this->service->delete($data));
    }
}